﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.SubPage
{
    public partial class uc_danh_muc_xe : System.Web.UI.UserControl
    {
        #region[Define]
        private DataTable dtVehicle;
        private DataTable dtTypeVehicle;
        private DataTable dtMember;
        private DataTable dtGroupVehicle;
        protected string SuggestionList;
        protected string StrAlert = "";
        private clsTMType_VehicleBusiness clsTypeVehicle = new clsTMType_VehicleBusiness();
        private clsTMVehicleBusiness clsVehicle = new clsTMVehicleBusiness();
        private clsTMMemberBusiness clsMember = new clsTMMemberBusiness();
        private clsTMVehicleGroupBusiness clsGroupVehicle = new clsTMVehicleGroupBusiness();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridTypeVehicle();
                BindGridMember();
                BindGridVehicleGroup();
            }
        }
       //---------------button-popup---------------------
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {
            RefeshDropdownlist();
        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtSoXe.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào số xe !";
                OpenDialog("#ucthongbaodanhmucxe");
                updatePanelThongBao.Update();
            }
            if (txtNgayDangKiem.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền chọn ngày đăng kiếm !";
                OpenDialog("#ucthongbaodanhmucxe");
                updatePanelThongBao.Update();
            }
            if (drLoaiXe.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn loại xe !";
                OpenDialog("#ucthongbaodanhmucxe");
                updatePanelThongBao.Update();
            }
            if (drNhomXe.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn nhóm xe !";
                OpenDialog("#ucthongbaodanhmucxe");
                updatePanelThongBao.Update();
            }
            if (drTaiXe.SelectedValue.ToString() == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn tài xế !";
                OpenDialog("#ucthongbaodanhmucxe");
                updatePanelThongBao.Update();
            }
            if (txtNgayHetHang.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn ngày hết hạn !";
                OpenDialog("#ucthongbaodanhmucxe");
                updatePanelThongBao.Update();
            }
            if (txtSoXe.Text != "" && txtNgayDangKiem.Text != "" && txtNgayHetHang.Text != "" && drLoaiXe.SelectedValue.ToString() != "-1" && drNhomXe.SelectedValue.ToString() != "-1" && drTaiXe.SelectedValue.ToString() != "-1")
            {
                InsertVehicle();
            }
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbaodanhmucxe");
        }
        protected void ImgRefeshDuyet_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void ImgRefeshExport_Click(object sender, ImageClickEventArgs e)
        {
            //txtTuNgayNhapKhoExport.Text = "";
            //txtDenNgayNhapKhoExport.Text = "";
            //BindGridLenhDieuDongExportExcel();
        }
        protected void drTrongTai_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        #endregion

        #region[Function]
        protected void BindGridTypeVehicle()
        {
            dtTypeVehicle = clsTypeVehicle.BindGirdTMType_Vehicle();
            if (dtTypeVehicle != null && dtTypeVehicle.Rows.Count > 0)
            {
                drLoaiXe.DataSource = dtTypeVehicle;
                drLoaiXe.DataTextField = "Type_Vehicle_Name";
                drLoaiXe.DataValueField = "Type_Vehicle_ID";
                drLoaiXe.DataBind();
            }
        }
        protected void BindGridMember()
        {
            dtMember = clsMember.BindGirdTMMember();
            if (dtMember != null && dtMember.Rows.Count > 0)
            {
                drTaiXe.DataSource = dtMember;
                drTaiXe.DataTextField = "FullNameMem";
                drTaiXe.DataValueField = "Member_ID";
                drTaiXe.DataBind();
            }
        }
        protected void BindGridVehicleGroup()
        {
            dtGroupVehicle = clsGroupVehicle.BindGirdTMVehicleGroup();
            if (dtGroupVehicle != null && dtGroupVehicle.Rows.Count > 0)
            {
                drNhomXe.DataSource = dtGroupVehicle;
                drNhomXe.DataTextField = "VehicleGroup_Name";
                drNhomXe.DataValueField = "VehicleGroup_ID";
                drNhomXe.DataBind();
            }
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            dtVehicle = clsVehicle.BindGirdTMVehicle();
            if (dtVehicle != null && dtVehicle.Rows.Count > 0)
            {
                foreach (DataRow dr in dtVehicle.Rows)
                {
                    if (txtSoXe.Text == dr["Vehicle_Number"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertVehicle()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Số xe này đã tồn tại !";
                    OpenDialog("#ucthongbaodanhmucxe");
                    updatePanelThongBao.Update();
                }
                else
                {
                    VanTaiProperty.clsTMVehicleProperty obj = new clsTMVehicleProperty();
                    obj.Vehicle_Number = txtSoXe.Text;
                    obj.Vehicle_Type_Vehicle_ID = int.Parse(drLoaiXe.SelectedValue.ToString());
                    obj.VehicleGroup_Old_ID = int.Parse(drNhomXe.SelectedValue.ToString());
                    obj.Member_ID = int.Parse(drTaiXe.SelectedValue.ToString());
                    obj.Date_Of_Reg = DateTime.Parse(txtNgayDangKiem.Text);
                    obj.Date_Of_Expires = DateTime.Parse(txtNgayHetHang.Text);
                    obj.Note_Vehicle = txtGhiChu.Text;
                    //---------------------
                    obj.Division_Old_ID = 0;
                    obj.Division_New_ID = 0;
                    obj.VehicleGroup_New_ID = 0;
                    obj.Decision_ID = 0;
                    obj.Create_Date_Vehicle = DateTime.Parse(txtNgayDangKiem.Text);
                    obj.Active_Vehicle = false;
                    obj.HMCT_ID = 0;
                    obj.MaCha = DBNull.Value.ToString();

                    VanTaiBLL.clsTMVehicleBusiness.InsertTMVehicle(obj);
                    lbThongBaoLoi.Text = "Thêm mẫu tin thành công !";
                    OpenDialog("#ucthongbaodanhmucxe");
                    updatePanelThongBao.Update();
                    ClearTextBox();
                    ClearDropdownlist();
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#ucthongbaodanhmucxe");
                updatePanelThongBao.Update();
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

        #region[Clear-TextBox]
        protected void ClearTextBox()
        {
            txtSoXe.Text = "";
            txtNgayDangKiem.Text = DateTime.Now.ToString();
            txtNgayHetHang.Text = Convert.ToDateTime(DateTime.Now.AddDays(60.0)).ToString("dd/MM/yyyy");
        }
        protected void ClearDropdownlist()
        {
            drLoaiXe.SelectedIndex = -1;
            drNhomXe.SelectedIndex = -1;
            drTaiXe.SelectedIndex = -1;
        }
        protected void RefeshDropdownlist()
        {
            drLoaiXe.Items.Clear();
            drLoaiXe.Items.Add(new ListItem("--Vui lòng chọn loại xe--", "-1"));
            BindGridTypeVehicle();

            drTaiXe.Items.Clear();
            drTaiXe.Items.Add(new ListItem("--Vui lòng chọn tài xế--", "-1"));
            BindGridMember();

            drNhomXe.Items.Clear();
            drNhomXe.Items.Add(new ListItem("--Vui lòng chọn nhóm xe--", "-1"));
            BindGridVehicleGroup();
        }
        #endregion
    }
}